package lk.service.blogspringboot.service.impl;

import lk.service.blogspringboot.domain.ArticleCollectionDetail;
import lk.service.blogspringboot.domain.ArticleInfo;
import lk.service.blogspringboot.mapper.ArticleCollectionDetailMapper;
import lk.service.blogspringboot.mapper.ArticleInfoMapper;
import lk.service.blogspringboot.service.ArticleCollectionDetailService;
import lk.service.blogspringboot.util.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 文章收藏详细信息表(ArticleCollectionDetail)表服务实现类
 *
 * @author LK
 * @since 2024-08-22 10:33:37
 */
@Service("articleCollectionDetailService")
public class ArticleCollectionDetailServiceImpl implements ArticleCollectionDetailService {
    @Resource
    private ArticleCollectionDetailMapper articleCollectionDetailMapper;

    @Resource
    private ArticleInfoMapper articleInfoMapper;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public R queryById(Integer id) {
        return R.ok().setData(this.articleCollectionDetailMapper.queryById(id));
    }

    /**
     * 全查询
     *
     * @param articleCollectionDetail 筛选条件
     * @return 查询结果
     */
    @Override
    public R queryAll(ArticleCollectionDetail articleCollectionDetail) {
        return R.ok().setData(this.articleCollectionDetailMapper.queryAll(articleCollectionDetail));
    }

    /**
     * 新增收藏量
     *
     * @param articleCollectionDetail 实例对象
     * @return 实例对象
     */
    @Override
    public R insert(ArticleCollectionDetail articleCollectionDetail) {
        this.articleCollectionDetailMapper.insert(articleCollectionDetail);
        // 新增之后 对应的文章的收藏量量字段数目+1
        ArticleInfo articleInfo = articleInfoMapper.queryById(articleCollectionDetail.getArticleId());
        articleInfo.setCollections(articleInfo.getCollections() + 1);
        articleInfoMapper.update(articleInfo);
        return R.ok().setData(articleCollectionDetail);
    }

    /**
     * 修改数据
     *
     * @param articleCollectionDetail 实例对象
     * @return 实例对象
     */
    @Override
    public R update(ArticleCollectionDetail articleCollectionDetail) {
        this.articleCollectionDetailMapper.update(articleCollectionDetail);
        return R.ok().setData(this.queryById(articleCollectionDetail.getId()));
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public R deleteById(Integer id) {
        boolean del = this.articleCollectionDetailMapper.deleteById(id) > 0;
        return R.ok().setData(del);
    }

    /**
     *  删除收藏量

     */
    @Override
    public R deleteByUserNameAndArticleId(Integer articleId, String collector) {
        if (this.articleCollectionDetailMapper.deleteByUserNameAndArticleId(articleId, collector)) {
            // 删除之后 对应的文章的收藏量量字段数目-1
            ArticleInfo articleInfo = articleInfoMapper.queryById(articleId);
            articleInfo.setCollections(articleInfo.getCollections() - 1);
            articleInfoMapper.update(articleInfo);
            return R.ok().setData(true);
        }else {
            return R.ok().setData(false);
        }
    }

    @Override
    public R queryIsStar(Integer articleId, String collector) {
        if (this.articleCollectionDetailMapper.queryIsStar(articleId, collector)) {
            return R.ok().setData(true);
        }else {
            return R.ok().setData(false);
        }
    }
}

